package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketAddress;
import com.market.bean.MarketAddressExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketAddressMapper;
import com.market.service.admin.AdminAddressService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tan
 * @date 2022年12月29日 14:31
 */
@Service
public class AdminAddressServiceImpl implements AdminAddressService {
    @Autowired
    MarketAddressMapper addressMapper;

    @Override
    public CommonData<MarketAddress> list(BasePageInfo info, String name, Integer userId) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据name和userId条件查询
        MarketAddressExample example = new MarketAddressExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketAddressExample.Criteria criteria = example.createCriteria();
        //判空 增加if之后成为条件查询
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null ) {
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);
        // List<MarketAddress> list = addressMapper.selectByExample(example);
        List<MarketAddress> list = addressMapper.selectByAddressExample(example);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }
}

